using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
 
    public class StringUtils
    {

        /**
         * Convierte la cadena null a vacio
         * @param cad cadena a transformar
         * @return cadena vacia si es null
         */
        public static String stringNullToBlank(String cad)
        {
            return cad != null ? cad : "";
        }

        public static Int32 RMS_CANTREG = 5;


        public static int getDiaDeLaSemana()
        {
            int dia = (int)DateTime.Now.DayOfWeek;
            if (dia == 0) dia = 7;
            return dia;
        }


        public static byte[] comprimir(byte[] data)
        {
            MemoryStream stream = new MemoryStream();
            ZipOutputStream oZipStream = new ZipOutputStream(stream);

            ZipEntry entry = new ZipEntry("datos.bin");
            entry.Size = data.Length;
            oZipStream.PutNextEntry(entry);
            oZipStream.Write(data, 0, data.Length);
            oZipStream.CloseEntry();
            oZipStream.Finish();
            oZipStream.Close();

            byte[] oBuffer = stream.ToArray();
            return oBuffer;
        }



        public static byte[] desComprimir(byte[] data)
        {
            MemoryStream mem = new MemoryStream(data);
            ZipInputStream zin = new ZipInputStream(mem);
            ZipEntry zen = zin.GetNextEntry();
            byte[] buf = new byte[zen.Size];
            zin.Read(buf, 0, buf.Length);
            return buf;
        }

        public static bool isValid(int day, int month, int year)
        {
            if (!(month >= 1 && month <= 12)
            || !(day >= 1 && day <= 31)
            || !(year >= 1000 && year <= 9999)
            || (day == 31 &&
            (month == 4 /*april*/ || month == 6 /*june*/ ||
            month == 9 /*sept*/ || month == 11 /*nov*/ )) ||
            (month == 2 /*feb*/ && day > 29))
            {
                return false;
            }
            if (month == 2 && day == 29)
            { // if a year is divisible by 4 it is a leap year UNLESS it is also
                // divisible by 100 AND is not divisible by 400
                if (year % 4 > 0
                || ((year % 100 == 0) && (year % 400 > 0)))
                {
                    return false;
                }
            }
            return true;
        }

        public static bool validarFechaCompleta(string fecha)
        {
            bool  flag = false;
            if (fecha != null && fecha.Length >= 8)
            {
                string[] arrElementosFecha = fecha.Split(' ');
                if (arrElementosFecha.Length > 0)
                {
                    flag = validaFecha(arrElementosFecha[0]);
                }
                if (flag && arrElementosFecha.Length > 1)
                {
                    flag = validarHoraMinuto(arrElementosFecha[1]);
                }                
            }
            return flag;
        }

        private static bool validaFecha(string fecha)
        {
            int ele1 = 0; int ele2 = 0;int ele3 = 0;

            if (fecha != null && fecha.Length >= 8)
            {
                string[] afecha = fecha.Split('/');
                if (afecha.Length > 0 && validarNumeroEntero(afecha[0]))
                {
                    ele1 = int.Parse(afecha[0]);
                }

                if (afecha.Length > 1 && validarNumeroEntero(afecha[1]))
                {
                    ele2 = int.Parse(afecha[1]);
                }

                if (afecha.Length > 2 && validarNumeroEntero(afecha[2]))
                {
                    ele3 = int.Parse(afecha[2]);
                }
                if (ele1 > 0 && ele2 > 0 && ele3 > 0)
                {
                    switch (ManagerConfiguration.formatofecha)
                    {
                        case ManagerConfiguration.FORMATODDMMYY:
                            return isValid(ele1,ele2,ele3);  
                        case ManagerConfiguration.FORMATOMMDDYY:
                            return isValid(ele2, ele1, ele3); 
                        default:  return isValid(ele1,ele2,ele3);  
                    }
                }
            }
            return false;
        }


        public static string getFechaActual( )
        {
             string dia = DateTime.Now.Day.ToString().PadLeft(2,'0');
             string mes = DateTime.Now.Month.ToString().PadLeft(2, '0');
             string anio = DateTime.Now.Year.ToString().PadLeft(4, '0'); 

             switch (ManagerConfiguration.formatofecha)
             {
                 case ManagerConfiguration.FORMATODDMMYY:
                     return dia + "/" + mes + "/" + anio;
                 case ManagerConfiguration.FORMATOMMDDYY:
                     return mes + "/" + dia + "/" + anio;
                 default: return dia + "/" + mes + "/" + anio;
             }
            
        }

      



        public static bool validarHoraMinuto(String horapara)
        {
            bool flag = true;
            try
            {
                string[] arrElementosFecha = horapara.Split(':');
                int hora = int.Parse(arrElementosFecha[0]);
                int minuto = int.Parse(arrElementosFecha[1]);


                if (hora >= 0 && hora < 24 && minuto >= 0 && minuto < 60)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            catch (Exception e)
            {
             
                flag = false;
            }
            return flag;
        }


        public static Boolean validarNumeroEntero(String numero)
        {
            Boolean flag = true;
            try
            {
                Convert.ToInt32(numero);

            }
            catch (Exception e)
            {
            
                flag = false;
            }
            return flag;
        }

        public static Boolean validarNumeroEntero64(String numero)
        {
            Boolean flag = true;
            try
            {
                Convert.ToInt64(numero);

            }
            catch (Exception e)
            {

                flag = false;
            }
            return flag;
        }

        public static Boolean validarNumeroDecimal(String numero)
        {
            Boolean flag = true;

            if (!(numero != null && numero != "") ){
                return false;
            }

            try
            {
                Convert.ToDouble(numero);
            }
            catch (Exception e)
            {
                flag = false;
            }
            return flag;
        }

        public static string[] getFechaArray(string fecha)
        {
            string[] array = { "", "00:00" };

            if (fecha != null && !"".Equals(fecha))
            {
                string[] split = fecha.Split(' ');
                if (split.Length > 0)
                {
                    array[0] = split[0];
                }

                if (split.Length > 1)
                {
                    array[1] = split[1];
                }

            }

            return array;

        }
 
}






